
package com.j2eefast.bussiness.contract.controller;

import com.j2eefast.bussiness.contract.entity.BusEquipmentEntity;
import com.j2eefast.bussiness.contract.entity.BusInstallsEntity;
import com.j2eefast.bussiness.contract.entity.BusMeteringUnitEntity;
import com.j2eefast.bussiness.contract.service.BusEquipmentService;
import com.j2eefast.bussiness.contract.service.BusInstallsService;
import com.j2eefast.bussiness.contract.service.BusMeteringUnitService;
import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.sys.service.SysUserService;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;


/**
 * <p>选项设置</p>
 *
 * @author: summer
 * @date: 2021-06-01 10:41
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/bus/installs")
public class BusInstallsController extends BaseController {

	private String urlPrefix = "modules/bus/installs";
	@Autowired
	private BusMeteringUnitService busMeteringUnitService;
	@Autowired
	private BusInstallsService busInstallsService;
	@Autowired
	private SysUserService sysUserService;

	@RequiresPermissions("bus:installs:view")
	@GetMapping()
	public String process() {
		return urlPrefix + "/index";
	}

	@RequestMapping("/list")
	@RequiresPermissions("bus:installs:list")
	@ResponseBody
	public ResponseData list(@RequestParam Map<String, Object> params) {
		PageUtil page = busInstallsService.findPage(params);
		return success(page);
	}

	/**
	 * 删除
	 */
	@BussinessLog(title = "删除", businessType = BusinessType.DELETE)
	@RequestMapping( value = "/del", method = RequestMethod.POST)
	@RequiresPermissions("bus:installs:del")
	@RequiresRoles(Constant.SU_ADMIN)
	@ResponseBody
	public ResponseData delete(Long[] ids) {
		return busInstallsService.deleteByIds(ids)?success(): error("删除失败!");
	}


	/**
	 * 修改出入库 页面
	 */
	@RequiresPermissions("bus:installs:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap) {
		BusInstallsEntity busInstallsEntity = busInstallsService.getById(id);

		mmap.put("installs", busInstallsEntity);
		return urlPrefix + "/edit";
	}

	/**
	 * 修改出入库保存
	 */
	@RepeatSubmit
	@RequiresPermissions("bus:installs:edit")
	@BussinessLog(title = "修改", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData edit(BusInstallsEntity busInstallsEntity) {
		ValidatorUtil.validateEntity(busInstallsEntity);
		Date date = new Date();//创建一个时间对象，获取到当前的时间
		busInstallsEntity.setUpdateTime(date);
//		Long userId= UserUtils.getUserInfo().getId();
//		SysUserEntity sysUserEntity=busInstallsService.getSysById(userId);
//		busInstallsEntity.setUpdateBy(sysUserEntity.getName());
		String userName=UserUtils.getUserInfo().getName();
		busInstallsEntity.setCreateBy(userName);
		return busInstallsService.updateById(busInstallsEntity) ? success() : error("修改失败!");
	}

	/**
	 * 新增参数配置
	 */
	@RequiresPermissions("bus:installs:add")
	@GetMapping("/add/{type}")
	public String add( @PathVariable("type") String type, ModelMap mmap) {
		mmap.put("type", type);
		return urlPrefix + "/add";
	}



	/**
	 * 新增保存
	 */
	@RepeatSubmit
	@RequiresPermissions("bus:installs:add")
	@BussinessLog(title = "认领", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData addSave(@Validated BusInstallsEntity busInstallsEntity) {
		//校验参数
		ValidatorUtil.validateEntity(busInstallsEntity);
		Date date = new Date();//创建一个时间对象，获取到当前的时间
		//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
		busInstallsEntity.setCreateTime(date);
//		Long userId= UserUtils.getUserInfo().getId();
//		SysUserEntity sysUserEntity=busInstallsService.getSysById(userId);
//		busInstallsEntity.setCreateBy(sysUserEntity.getName());
		String userName=UserUtils.getUserInfo().getName();
		busInstallsEntity.setCreateBy(userName);
		return busInstallsService.save(busInstallsEntity) ? success() : error("新增失败!");
	}

	/**
	 * 修改出入库保存
	 */

	@RequiresPermissions("bus:installs:checkout")
	@BussinessLog(title = "出库", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/checkout", method = RequestMethod.POST)
	@ResponseBody
//	@GetMapping("/checkout/{id}")
	public ResponseData checkout(String id, ModelMap mmap) {
		System.out.println("1111111111111111111111");
		ValidatorUtil.validateEntity(id);
		Long ids=Long.parseLong(id);
		return busInstallsService.updateCheck(ids) ? success() : error("修改失败!");
	}





}
